package fr.gdi.android.news;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import fr.gdi.android.news.utils.Constants;
import fr.gdi.android.news.utils.FeedUtils;
import fr.gdi.android.news.utils.PreferenceUtils;
import fr.gdi.android.news.utils.Utils;

public class DialogSettingsActivity extends ListActivity
{
    private static final class WidgetConfiguration
    {
        Integer id;
        String title;
        String[] feeds;
    }
    
    private static class EfficientAdapter extends BaseAdapter 
    {
        private LayoutInflater mInflater;
        //private Bitmap configureButton;
        //private Bitmap refreshButton;
        private Context context;
        
        private List<WidgetConfiguration> list = new ArrayList<WidgetConfiguration>();
        
        public EfficientAdapter(Context context, List<WidgetConfiguration> list) 
        {
            // Cache the LayoutInflate to avoid asking for a new one each time.
            mInflater = LayoutInflater.from(context);

            // Icons bound to the rows.
           // configureButton = BitmapFactory.decodeResource(context.getResources(), R.drawable.widget_button_normal);
           // refreshButton = BitmapFactory.decodeResource(context.getResources(), R.drawable.widget_refresh_icon);
            
            this.list = list;
            this.context = context;
        }

        @Override
        public int getCount()
        {
            return list.size();
        }

        @Override
        public Object getItem(int position)
        {
            return position;
        }

        @Override
        public long getItemId(int position)
        {
            return position;
        }

        @Override
        public boolean areAllItemsEnabled()
        {
            return true;
        }
        
        @Override
        public View getView(final int position, View convertView, ViewGroup parent)
        {
         // A ViewHolder keeps references to children views to avoid unneccessary calls
            // to findViewById() on each row.
            ViewHolder holder;

            // When convertView is not null, we can reuse it directly, there is no need
            // to reinflate it. We only inflate a new View when the convertView supplied
            // by ListView is null.
            if (convertView == null) 
            {
                convertView = mInflater.inflate(R.layout.settings_dialog_item, null);

                // Creates a ViewHolder and store references to the two children views
                // we want to bind data to.
                holder = new ViewHolder();
                
                holder.title = (TextView) convertView.findViewById(R.id.widget_title_text);
                holder.feed = (TextView) convertView.findViewById(R.id.widget_feed_list);
                holder.configureIcon = (ImageView) convertView.findViewById(R.id.feed_edit_button);
                holder.refreshIcon = (ImageView) convertView.findViewById(R.id.feed_refresh_button);
                
                convertView.setTag(holder);
                
             //   holder.configureIcon.setFocusable(true);
             //   holder.configureIcon.setClickable(true);
             //   convertView.setClickable(true);
                holder.configureIcon.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view)
                    {
                        //Integer position = ((ViewHolder) view.getTag()).position;
                        Log.e("fr.gdi.android.news", "ONCLICK");
                    }
                    
                });
                
                holder.configureIcon.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view)
                    {
                        Intent intent = new Intent(context, ConfigurationActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, list.get(position).id);
                        intent.putExtra(Constants.Configuration.LIVE_WIDGET, true);
                        context.startActivity(intent);
                    }
                    
                });
                
                holder.refreshIcon.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view)
                    {
                        UpdateService.update(context, list.get(position).id);
                    }
                    
                });
            } 
            else 
            {
                // Get the ViewHolder back to get fast access to the TextView
                // and the ImageView.
                holder = (ViewHolder) convertView.getTag();
            }

            
            // Bind the data efficiently with the holder.
            holder.title.setText(list.get(position).title);
            holder.feed.setText(TextUtils.join(", ", list.get(position).feeds)) ;
           // holder.refreshIcon.setImageBitmap(refreshButton);
           // holder.configureIcon.setImageBitmap(configureButton);

            return convertView;

        }
        
        

        
        static final class ViewHolder {
            TextView title;
            TextView feed;
            ImageView refreshIcon;
            ImageView configureIcon;
        }

    }
    
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.settings_dialog);
        
        EfficientAdapter adapter = getAdapter();
        setListAdapter(adapter);
    }


    
    private EfficientAdapter getAdapter()
    {
        int[] widgetIds = Utils.getAllWidgetIds(this);
        
        List<WidgetConfiguration> list = new ArrayList<WidgetConfiguration>();
        if ( widgetIds.length > 0 )
        {
            for (int i = 0; i < widgetIds.length; i++)
            {
                int id = widgetIds[i];
                List<String> feeds = FeedUtils.getFeedNames(this, id);
                String title = PreferenceUtils.getWidgetTitle(this, id);
                if ( feeds.size() > 0 ) 
                {
                    WidgetConfiguration c = new WidgetConfiguration();
                    c.title = title;
                    c.feeds = feeds.toArray(new String[feeds.size()]);
                    c.id = id;
                    list.add(c);
                }
            }
        }
        
        EfficientAdapter adapter = new EfficientAdapter(this, list);
        return adapter;
    }
    
}
